import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mall/constant/app_dimens.dart';

import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/text_style.dart';
import 'package:mall/gen/r.dart';
import 'package:mall/modules/model/coupon_entity.dart';
import 'package:mall/modules/sc/coupon_sc.dart';
import 'package:mall/utils/list_placeholder_component/list_placeholder_component.dart';
import 'package:mall/utils/refresh/refresh_widget.dart';
import 'package:mall/utils/refresh/warp_refresh_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CouponPage extends StatefulWidget {
  @override
  _CouponPageState createState() => _CouponPageState();
}

class _CouponPageState extends State<CouponPage> {
  final sc_couponList = Get.put(CouponListSc());
  late final _refreshController = WarpRefreshController(loadState: sc_couponList.loadState);

  // CouponViewModel _couponViewModel = CouponViewModel();
  // RefreshController _refreshController = RefreshController();
  // int _pageIndex = 1;
  // int _pageSize = 20;

  // @override
  // void initState() {
  //   super.initState();
  //   _couponViewModel.queryCoupon(_pageIndex, _pageSize);
  // }
  @override
  void dispose() {
    super.dispose();
    _refreshController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(AppStrings.MINE_COUPON),
        centerTitle: true,
      ),
      body: _contentView(),
      // body: ChangeNotifierProvider(
      //   create: (_) => _couponViewModel,
      //   child: Consumer<CouponViewModel>(builder: (context, model, child) {
      //     _pageIndex = model.canLoadMore ? ++_pageIndex : _pageIndex;
      //     RefreshStateUtil.getInstance()
      //         .stopRefreshOrLoadMore(_refreshController);
      //     return _initView(model);
      //   }),
      // ),
    );
  }

  // Widget _initView(CouponViewModel couponViewModel) {
  //   if (couponViewModel.pageState == PageState.hasData) {
  //     return _contentView(couponViewModel);
  //   }
  //   return ViewModelStateWidget.stateWidgetWithCallBack(
  //       couponViewModel, _onRefresh);
  // }

  // void _onRefresh() {
  //   _pageIndex = 1;
  //   _couponViewModel.queryCoupon(_pageIndex, _pageSize);
  // }

  // void _onLoadMore() {
  //   _couponViewModel.queryCoupon(_pageIndex, _pageSize);
  // }

  Widget _contentView() {
    return Container(
      // child: SmartRefresher(
      //     enablePullDown: true,
      //     enablePullUp: addressViewModel.canLoadMore,
      //     header: WaterDropMaterialHeader(
      //       backgroundColor: AppColors.COLOR_FF5722,
      //     ),
      //     controller: _refreshController,
      //     onLoading: () => _onLoadMore(),
      //     onRefresh: () => _onRefresh(),
      //     child: ListView.builder(
      //         itemCount: addressViewModel.coupons.length,
      //         itemBuilder: (BuildContext context, int index) {
      //           return _couponItemView(addressViewModel.coupons[index]);
      //         })),
      child: Obx(
        () => RefreshComponent(
          controller: _refreshController,
          onLoading: () => sc_couponList.loadMoreData(),
          onRefresh: () => sc_couponList.refreshData(),
          child: WrappedListPlaceholderComponent.widget(
            sc: sc_couponList,
            builder: (p0) => ListView.builder(
                itemCount: p0.length,
                itemBuilder: (BuildContext context, int index) {
                  return _couponItemView(p0[index]);
                }),
          ),
        ),
      ),
    );
  }

  Widget _couponItemView(CouponList coupon) {
    return Container(
        child: Container(
      margin: EdgeInsets.only(left: 15, right: 15, top: 10),
      height: ScreenUtil().setHeight(AppDimens.DIMENS_270),
      decoration: BoxDecoration(image: DecorationImage(image: AssetImage(R.image.bgCoupon.path), fit: BoxFit.fill)),
      alignment: Alignment.center,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Container(
            width: ScreenUtil().setWidth(AppDimens.DIMENS_240),
            alignment: Alignment.center,
            margin: EdgeInsets.only(left: 22.5, right: 22.5),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Text(
                  AppStrings.DOLLAR,
                  style: FMTextStyle.color_ff5722_size_13,
                ),
                Text(
                  "${coupon.discount}",
                  style: FMTextStyle.color_ff5722_size_26,
                )
              ],
            ),
          ),
          Expanded(
              child: Container(
            alignment: Alignment.centerLeft,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  (coupon.name ?? "") + (coupon.desc ?? ""),
                  style: FMTextStyle.color_222222_size_13,
                ),
                Padding(padding: EdgeInsets.only(top: 10)),
                Text(
                  AppStrings.FULL + "${coupon.min}" + AppStrings.USE,
                  style: FMTextStyle.color_222222_size_13,
                ),
                Padding(padding: EdgeInsets.only(top: 10)),
                Text(
                  transformDate(coupon.startTime ?? "") + "-" + transformDate(coupon.endTime ?? ""),
                  style: FMTextStyle.color_222222_size_13,
                ),
              ],
            ),
          ))
        ],
      ),
    ));
  }

  String transformDate(String time) {
    var data = DateTime.parse(time);
    return DateUtil.formatDate(data, format: DateFormats.y_mo_d);
  }
}
